package rp.lee.base.table;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import rp.lee.base.DBTable;
import rp.lee.model.MusicInfo;
import rp.lee.utils.SQLiteHelper;

public class MusicInfoTable implements DBTable {

	public static final String TABLENAME = "music_info";
	
	public static final String NAME = "name";
	public static final String SINGER = "singer";
	public static final String ALBUM = "album";
	public static final String TIME = "time";
	public static final String SIZE = "size";
	public static final String TYPE = "type";
	public static final String RATING = "rating";
	public static final String PATH = "path";
	
	@Override
	public String getCreateTableSql() {
		return "create table if not exists "+ TABLENAME +"("
				+ _ID + " integer primary key,"
				+ NAME + " varchar,"
				+ SINGER + " varchar,"
				+ ALBUM + " varchar,"
				+ TIME + " integer,"
				+ TYPE + " varchar,"
				+ PATH + " varchar,"
				+ SIZE + " varchar,"
				+ RATING + " integer)";
	}

	@Override
	public String getTableName() {
		return TABLENAME;
	}

	public boolean delete(SQLiteHelper sqlHelper, int _id) {
		SQLiteDatabase sdb = sqlHelper.getWritableDatabase();
		int r = sdb.delete(TABLENAME, " _id = " + _id, null);
		return r > 0;
	}
	public boolean insert(SQLiteHelper sqlHelper, MusicInfo info) {
		
		SQLiteDatabase sdb = sqlHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(NAME, info.getName());
		values.put(SINGER, info.getArtist());
		values.put(ALBUM, info.getAlbum());
		values.put(TIME, info.getTime());
		values.put(TYPE, info.getType());
		values.put(PATH, info.getPath());
		values.put(SIZE, info.getSize());
		values.put(RATING, info.getRating());
		
		long l = sdb.insert(TABLENAME, _ID, values);
//		sdb.close();
		return l > 0l;
	}
	
	public List<MusicInfo> query(SQLiteHelper sqlHelper) {
		
		List<MusicInfo> musicInfoList = new ArrayList<MusicInfo>();
		SQLiteDatabase sdb = sqlHelper.getReadableDatabase();
		Cursor c = sdb.query(MusicInfoTable.TABLENAME, null, null, null, null, null, null);
		MusicInfo info = null;
		
		int _idIndex = c.getColumnIndex(_ID);
		int nameIndex = c.getColumnIndex(NAME);
		int singerIndex = c.getColumnIndex(SINGER);
		int albumIndex = c.getColumnIndex(ALBUM);
		int timeIndex = c.getColumnIndex(TIME);
		int typeIndex = c.getColumnIndex(TYPE);
		int ratingIndex = c.getColumnIndex(RATING);
		int pathIndex = c.getColumnIndex(PATH);
		int sizeIndex = c.getColumnIndex(SIZE);
		
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			
			info = new MusicInfo();
			info.set_id(c.getInt(_idIndex));
			info.setTime(c.getInt(timeIndex));
			info.setRating(c.getInt(ratingIndex));
			info.setSize(c.getInt(sizeIndex));
			
			info.setName(c.getString(nameIndex));
			info.setArtist(c.getString(singerIndex));
			info.setAlbum(c.getString(albumIndex));
			info.setType(c.getString(typeIndex));
			info.setPath(c.getString(pathIndex));
			
			musicInfoList.add(info);
		}
		
//		sdb.close();
		return musicInfoList;
	}
}